package guiLayer;

import guiLayer.extensions.TreeCellRender;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import modelLayer.Forest;
import modelLayer.Tree;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controlerLayer.ForestCtr;

public class ReadTree extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JList<Tree> list;
	
	/**
	 * Create the panel.
	 */
	public ReadTree() {
		addAncestorListener(new AncestorListener() {
			public void ancestorAdded(AncestorEvent arg0) {
				redraw();
			}
			public void ancestorMoved(AncestorEvent arg0) {
			}
			public void ancestorRemoved(AncestorEvent arg0) {
			}
		});
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
				ColumnSpec.decode("145dlu"),
				ColumnSpec.decode("111dlu:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,}));
		
		JPanel panel = new JPanel();
		add(panel, "2, 2, fill, fill");
		panel.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane);
		
		list = new JList<Tree>();
		TreeCellRender render = new TreeCellRender();
		list.setCellRenderer(render);
		scrollPane.setViewportView(list);
		
		JPanel panel_1 = new JPanel();
		add(panel_1, "3, 2, fill, fill");
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[]{0, 0};
		gbl_panel_1.rowHeights = new int[]{92, 0, 0};
		gbl_panel_1.columnWeights = new double[]{0.0, Double.MIN_VALUE};
		gbl_panel_1.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		panel_1.setLayout(gbl_panel_1);
		
		JPanel panel_3 = new JPanel();
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.insets = new Insets(0, 0, 5, 0);
		gbc_panel_3.fill = GridBagConstraints.BOTH;
		gbc_panel_3.gridx = 0;
		gbc_panel_3.gridy = 0;
		panel_1.add(panel_3, gbc_panel_3);
		panel_3.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("80px:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("80px:grow"),},
			new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JButton btnNewButton_1 = new JButton("Update");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(list.getSelectedValue() != null){
					ArrayList<Object> selected = new ArrayList<Object>();
					selected.addAll(list.getSelectedValuesList());
					update(selected);
				}
			}
		});
		panel_3.add(btnNewButton_1, "2, 1");
		
		JButton btnNewButton = new JButton("Remove");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(list.getSelectedValue() != null){
					remove(list.getSelectedValue());
				}
			}
		});
		panel_3.add(btnNewButton, "4, 1");
		
		JPanel panel_2 = new JPanel();
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.fill = GridBagConstraints.BOTH;
		gbc_panel_2.gridx = 0;
		gbc_panel_2.gridy = 1;
		panel_1.add(panel_2, gbc_panel_2);
		
	}

	protected void remove(Object tree) {
		if(tree instanceof Tree){
			ForestCtr fCtr = new ForestCtr();
			fCtr.remove((Tree)tree);
			redraw();
		}
	}

	protected void redraw() {
		Thread t = new Thread(){
			public void run(){
				ForestCtr fCtr = new ForestCtr();
				ArrayList<Tree> trees = new ArrayList<Tree>();
				ArrayList<Forest> forests = fCtr.getAllForests();
				for(Forest f : forests){
					trees.addAll(f.getTrees());
				}
		
				DefaultListModel<Tree> model = new DefaultListModel<Tree>();
				for(Tree t : trees){
					model.addElement(t);
				}
				
				list.setModel(model);
			}
		};
		
		t.start();
	}

	public void updateData() {
		redraw();
	}
	
	public void update(ArrayList<Object> selected){
		Updater u = new Updater(selected, this);
		u.setVisible(true);
		updateData();
	}
	
}
